package com.ninong.ker.admin.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ninong.ker.admin.domain.FwFormConfig;
import com.ninong.ker.admin.domain.FwGroupData;
import com.ninong.ker.admin.mapper.FwFormConfigMapper;
import com.ninong.ker.admin.service.FwFormConfigService;
import com.ninong.ker.admin.service.dto.FwFormConfigDTO;
import com.ninong.ker.common.auth.SecurityAdmin;
import com.ninong.ker.common.user.OnlineUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


/**
 * @author jmz
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class FwFormConfigServiceImpl extends ServiceImpl<FwFormConfigMapper, FwFormConfig> implements FwFormConfigService {

	@Autowired
	private SecurityAdmin securityAdmin;
    @Override
	public PageInfo<FwFormConfig> listPage(FwFormConfigDTO dto) {
		PageHelper.startPage(dto.getCurrentPage(), dto.getPageSize());
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		if (StringUtils.isNotEmpty(dto.getName())){
			query.eq(FwFormConfig::getName,dto.getName());
		}
		List<FwFormConfig> formConfigs = baseMapper.selectList(query);
		return new PageInfo<FwFormConfig>(formConfigs);
	}

	@Override
	public void delete(String[] ids) {
		List<String> idList = Arrays.asList(ids);
		idList.forEach(id -> {		
			FwFormConfig entity = new FwFormConfig();
			entity.setId(id);
			baseMapper.updateById(entity);
		});
	}

	@Override
	public void create(FwFormConfig entity) {
		OnlineUser onlineUser = securityAdmin.currentUser();
		entity.setTenantId(onlineUser.getTenantId());
		saveOrUpdate(entity);
	}

	@Override
	public FwFormConfig get(String id) {
		FwFormConfig entity = baseMapper.selectById(id);
		if (ObjectUtils.isEmpty(entity)) {
			entity = new FwFormConfig();
		}
		return entity;
	}

	@Override
	public List<FwFormConfig> treeForm() {
		LambdaQueryWrapper<FwFormConfig> query = new LambdaQueryWrapper<>();
		query.select(FwFormConfig::getId,FwFormConfig::getName);
		return baseMapper.selectList(query);
	}

}